package com.hl.sfzy.controller;

import com.hl.sfzy.constant.Result;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@CrossOrigin
@RestController
public class FileController {
    /**
     * 文件上传
     *
     * @param file    封装异步提交过来的文件数据的对象
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("/fileUpload")
    public Result fileUpload(MultipartFile file, HttpSession session) {
// 获取上传的文件的文件名
        String fileName = file.getOriginalFilename();
// 处理文件重名问题: 截取文件扩展名，用UUID替代文件名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID().toString() + suffix;
// 获取服务器中upload目录的路径
        ServletContext servletContext = session.getServletContext();
        String rootPath = servletContext.getRealPath("/upload"); // 获取upload文
// 当前日期文件夹
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateDirPath = sdf.format(new Date());

// 存储文件的目标目录
        //"/"

        // 存储文件的目标目录

        File dest = new File(rootPath + "/" + dateDirPath);
        // 判断目录是否存在
        if (!dest.exists()) {
            dest.mkdirs();
        }

        // 文件上传的最终路径
        String finalPath = rootPath + "/" + dateDirPath +
                "/" + fileName;
        //实现上传功能
        try {
            file.transferTo(new File(finalPath));
            return Result.success("/upload" + "/" + dateDirPath +
                    "/" + fileName);
        } catch (IOException e) {
            return Result.error("文件上传错误!");
        }
    }

    /**
     * 文件下载
     *
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("/fileDownload")
    public void fileDownload(String filePath, HttpServletResponse response,
                             HttpSession session) throws IOException {
// 获取服务器中文件的真实路径
        ServletContext servletContext = session.getServletContext();
        String realPath = servletContext.getRealPath(filePath);
// 创建输入流，读取文件
        FileInputStream fis = new FileInputStream(realPath);
// 设置响应头信息
        response.setHeader("Content-Disposition", "attachement;filename=" +
                realPath.substring(realPath.lastIndexOf("/")));

        // 获取输出流

        ServletOutputStream out = response.getOutputStream();
        // 拷贝
        IOUtils.copy(fis, out);
    }

    /**
     * 文件预览
     *
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("/filePreview")
    public void filePreview(String filePath, HttpServletResponse response,
                            HttpSession session) throws IOException {
        // 获取服务器中文件的真实路径
        ServletContext servletContext = session.getServletContext();
        String realPath = servletContext.getRealPath(filePath);
        // 创建输入流，读取文件
        FileInputStream fis = new FileInputStream(realPath);
        // 设置响应头信息
        response.setHeader("Content-Disposition", "filename=" +
                realPath.substring(realPath.lastIndexOf("/")));
        // 获取输出流
        ServletOutputStream out = response.getOutputStream();
        // 拷贝
        IOUtils.copy(fis, out);
    }
}